<?php

namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 短信验证码
 */
class UserScoreLog extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;

    // 追加属性
    protected $append = [
    ];

    public function user()
    {
        return $this->belongsTo('User', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getCreateTimeAttr($value)
    {
        return is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value;
    }

    /**
     * 每日登录奖励
     * @param $userId
     * @return bool
     */
    public static function loginBonus($userId)
    {
        $score = config('site.login_nums');
        if ($score <= 0) {
            return false;
        }

        // 启动事务
        Db::startTrans();
        try {

            $user = User::get($userId);
            $user->score += $score;
            $user->save();
            $before = $user->score - $score;
            $sdata = [
                'user_id' => $userId,
                'record_type' => 1,
                'score' => $score,
                'before' => $before,
                'after' => $user->score,
                'memo' => '登录赠送积分'
            ];

            self::create($sdata);

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

    /**
     * 点赞赠送积分
     * @param $userId
     * @return bool
     */
    public static function praiseBonus($userId, $record_type = 1)
    {
        $score = config('site.praise_nums');
        if ($score <= 0) {
            return false;
        }

        $today = strtotime('today');
        if ($record_type == 1) {
            $row_info = UserDayScore::where('user_id', $userId)->where('type', 2)->where('createtime', '>=', $today)->find();
            $limits = config('site.praise_nums_limits');
            // 超出限制不再赠送
            if ($row_info && $row_info->score >= $limits) {
                return false;
            }
        }

        // 启动事务
        Db::startTrans();
        try {

            $user = User::get($userId);
            if ($record_type == 1) {
                $user->score += $score;
                $user->save();
                $before = $user->score - $score;
                $memo = '点赞赠送积分';
            } else {
                $score = min($user->score, $score);
                $user->score -= $score;
                $user->save();
                $before = $user->score + $score;
                $memo = '取消点赞扣除积分';
            }

            $sdata = [
                'user_id' => $userId,
                'record_type' => $record_type,
                'score' => $score,
                'before' => $before,
                'after' => $user->score,
                'memo' => $memo
            ];

            self::create($sdata);

            if ($record_type == 1) {
                UserDayScore::changeScore($userId, 2, $score, 1);
            } else {
                UserDayScore::changeScore($userId, 2, $score, 0);
            }

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

    /**
     * 评论赠送积分
     * @param $userId
     * @param int $record_type
     * @return bool
     */
    public static function commentBonus($userId, $record_type = 1)
    {
        $score = config('site.comment_nums');
        if ($score <= 0) {
            return false;
        }

        $today = strtotime('today');
        if ($record_type == 1) {
            $row_info = UserDayScore::where('user_id', $userId)->where('type', 3)->where('createtime', '>=', $today)->find();
            $limits = config('site.comment_nums_limits');
            // 超出限制不再赠送
            if ($row_info && $row_info->score >= $limits) {
                return false;
            }
        }

        // 启动事务
        Db::startTrans();
        try {

            $user = User::get($userId);
            if ($record_type == 1) {
                $user->score += $score;
                $user->save();
                $before = $user->score - $score;
                $memo = '评论赠送积分';
            } else {
                $score = min($user->score, $score);
                $user->score -= $score;
                $user->save();
                $before = $user->score + $score;
                $memo = '取消评论扣除积分';
            }

            $sdata = [
                'user_id' => $userId,
                'record_type' => $record_type,
                'score' => $score,
                'before' => $before,
                'after' => $user->score,
                'memo' => $memo
            ];

            self::create($sdata);

            if ($record_type == 1) {
                UserDayScore::changeScore($userId, 3, $score, 1);
            } else {
                UserDayScore::changeScore($userId, 3, $score, 0);
            }

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

    /**
     * 发布赠送积分
     * @param $userId
     * @param int $record_type
     * @return bool
     */
    public static function publishBonus($userId, $record_type = 1)
    {
        $score = config('site.publish_nums');
        if ($score <= 0) {
            return false;
        }
        $today = strtotime('today');
        if ($record_type == 1) {
            $row_info = UserDayScore::where('user_id', $userId)->where('type', 3)->where('createtime', '>=', $today)->find();
            $limits = config('site.publish_nums_limits');
            // 超出限制不再赠送
            if ($row_info && $row_info->score >= $limits) {
                return false;
            }
        }

        // 启动事务
        Db::startTrans();
        try {

            $user = User::get($userId);
            if ($record_type == 1) {
                $user->score += $score;
                $user->save();
                $before = $user->score - $score;
                $memo = '发布赠送积分';
            } else {
                $score = min($user->score, $score);
                $user->score -= $score;
                $user->save();
                $before = $user->score + $score;
                $memo = '取消发布扣除积分';
            }

            $sdata = [
                'user_id' => $userId,
                'record_type' => $record_type,
                'score' => $score,
                'before' => $before,
                'after' => $user->score,
                'memo' => $memo
            ];

            self::create($sdata);

            if ($record_type == 1) {
                UserDayScore::changeScore($userId, 4, $score, 1);
            } else {
                UserDayScore::changeScore($userId, 4, $score, 0);
            }

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }
}
